﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Mystery.SharePoint
{
	/// <summary>
	/// Extension methods for SPPersistedObject
	/// </summary>
	public static class SPPersistedObjectExtensions
	{
		/// <summary>
		/// Writes a value to the property bag
		/// </summary>
		/// <param name="obj"></param>
		/// <param name="key">The key of the property</param>
		/// <param name="value">The value of the property</param>
		/// <returns>True if the property bag was updated, otherwise false</returns>
		public static bool Write(this SPPersistedObject obj, string key, object value)
		{
			if (obj == null) throw new ArgumentNullException("obj");

			return obj.Properties.Write(key, value);
		}

		/// <summary>
		/// Reads a value from the property bag
		/// </summary>
		/// <param name="obj"></param>
		/// <param name="key">The property key used to find the value</param>
		/// <param name="defaultValue">A value to use if the key doesn't exist in the property bag</param>
		/// <returns></returns>
		public static T Read<T>(this SPPersistedObject obj, string key, T defaultValue)
		{
			if (obj == null) throw new ArgumentNullException("obj");

			return obj.Properties.Read<T>(key, defaultValue);
		}
	}
}
